package com.echo.fm.admin.auto;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Repository;

import com.echo.fm.admin.auto.model.Admin;
import com.echo.util.ErrorException;
import com.echo.util.ResultUtils;

@Repository("adminPersistence")
public class DaoAdminPersistenceImpl implements IAdminPersistence {
	@Autowired
	@Qualifier("sessionFactory")
	protected SessionFactory sessionFactory;

	public Admin searchAdminUserByPhone(String phone) throws ErrorException {
		Session session = null;
		try {
			session = sessionFactory.openSession();

			Admin admin = (Admin) session.createQuery("from Admin where phone=:phone").setString("phone", phone)
					.uniqueResult();// 当返回结果只有一个时用uniqueResul
			return admin;

		} catch (HibernateException e) {
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public boolean checkPassword(String id, String password) throws ErrorException {
		Session session = null;

		try {
			session = sessionFactory.openSession();
			String query = "select count(1) from Admin where id= '%s' and password = '%s' ";
			query = String.format(query, id, password);
			org.hibernate.Query query2 = session.createSQLQuery(query);
			BigInteger count = (BigInteger) query2.uniqueResult();
			if (count.intValue() == 1) {
				return true;
			}
			return false;
		} catch (HibernateException e) {
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void registAdminUser(Admin admin) throws ErrorException {

		Session session = null;
		Transaction tx = null;

		try {
			session = sessionFactory.openSession();
			tx = session.beginTransaction();
			session.save(admin);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null) {
				session.close();
			}
		}

	}

	public void updateUserById(Admin admin) throws ErrorException {
		Session session = null;
		Transaction tx = null;
		try {
			session = sessionFactory.openSession();
			tx = session.beginTransaction();
			session.update(admin);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null)
				session.close();

		}

	}

	public void deleteUser(Admin admin) throws ErrorException {
		Session session = null;
		Transaction tx = null;
		try {
			session = sessionFactory.openSession();
			tx = session.beginTransaction();
			session.delete(admin);
			tx.commit();
		} catch (HibernateException e) {
			tx.rollback();
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null)
				session.close();
		}

	}

	public List<Admin> searchAdminList(String phone, String userName, Integer max, Integer firstNumber)
			throws ErrorException {
		Session session = null;
		try {
			session = sessionFactory.openSession();
			StringBuffer stringBuffer = new StringBuffer();
			String query = "from Admin where 1=1";
			stringBuffer.append(query);
			if (phone != null) {
				String phoneQuery = "and phone=%s";
				stringBuffer.append(String.format(phoneQuery, phone));
			}
			if (userName != null) {
				String userNameQuery = "and userName=%s";
				stringBuffer.append(String.format(userNameQuery, userName));
			}
			@SuppressWarnings("unchecked")
			List<Admin> adminList = session.createQuery(stringBuffer.toString()).setMaxResults(max).list();
			return adminList;

		} catch (HibernateException e) {
			e.printStackTrace();
			throw new ErrorException(ResultUtils.WEB_DB_SERVER_ERROR_CODE);
		} finally {
			if (session != null)
				session.close();
		}

	}

}
